﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hypermarket_Serial_Port
{
    class CashierRegisterController
    {
        private Dictionary<string, CashierRegister> mCashierRegisters;
        private static CashierRegisterController mInstance;

        private CashierRegisterController()
        {
            mCashierRegisters = new Dictionary<string, CashierRegister>();
        }

        public static CashierRegisterController getInstance()
        {
            if (mInstance == null)
            {
                mInstance = new CashierRegisterController();
            }
            return mInstance;
        }

        //public List<string> StartOperation(string inputStr)
        //{
        //    List<string> outputStr = new List<string>();
        //    List<string> commandList = mPManager.DemoProtocal(inputStr);
        //    outputStr = Operation(commandList);
        //    return outputStr;
        //}

        #region Operation
        public List<string> Operation(List<string> MessageInput)
        {
            try
            {
                byte num_InfoType = Convert.ToByte(MessageInput[1]);
                switch (num_InfoType)
                {

                    #region CASE 1FH/31 Cashier Login
                    case (byte)STATE.Authentication:
                        CashierRegisterTaskManager mLogInManager = new LogInManager();
                        return mLogInManager.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 25H/37 Add Product Transaction
                    case (byte)STATE.TransactionVerify:
                        CashierRegisterTaskManager mAddTransactionManager = new AddTransactionManager();
                        return mAddTransactionManager.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 45H/69 Cancel a Transaction
                    case (byte)STATE.DeleteVerify:
                        CashierRegisterTaskManager mCancelTransactionMgr = new CancelTransactionManager();
                        return mCancelTransactionMgr.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 5FH/95 Cancel All Transaction
                    case (byte)STATE.CancellationVerify:
                        CashierRegisterTaskManager mCancelAllTransactionMgr = new CancelAllTransactionManager();
                        return mCancelAllTransactionMgr.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 7FH/127 Cancel last Transaction
                    case (byte)STATE.DeleteLastItemVerifyState:
                        CashierRegisterTaskManager mCancelLastTransactionMgr = new CancelLastItemTransaction();
                        return mCancelLastTransactionMgr.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 75H/117 Request Last Item Information
                    case (byte)STATE.RequestLastItemInfo:
                        CashierRegisterTaskManager mRequestLastItemInfoMgr = new RequestLastItemInfoManager();
                        return mRequestLastItemInfoMgr.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 66H/102 Request Pice Information
                    case (byte)STATE.RequestPriceInfo:
                        CashierRegisterTaskManager mRequestPriceInfoMgr = new RequestPriceInfoManager();
                        return mRequestPriceInfoMgr.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion


                    #region CASE 3FH/63 Checkout iterms
                    case (byte)STATE.CheckOut:
                        CashierRegisterTaskManager mCheckOutManager = new CheckOutManager();
                        return mCheckOutManager.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion 


                    #region CASE 15H/21 LogOut
                    case (byte)STATE.LogOut:
                        CashierRegisterTaskManager mLogOut = new LogOutManager();
                        return mLogOut.TaskOperation(MessageInput, mCashierRegisters);
                    #endregion 

                    default:
                        SerialPortLog.LogOperation("not a valid case");
                        List<string> output = new List<string>();
                        output.Add(Constant.FAIL);
                        return output;
                }
            }
            catch
            {
                throw;
            }
        }

        public void DeactiveCashierRegister(string machineId)
        {
            try
            {
                mCashierRegisters.Remove(machineId);
            }
            catch
            {
            }
        }
        #endregion
    }
}
